using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using FU.Capstones.IMS.Web.Silverlight.Popups;
using Microsoft.Practices.Prism.Commands;
using System.Linq;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class EmploymentTypeListViewModel : ViewModelBase
    {
        public EmploymentTypeListViewModel()
        {
            Model = new EmploymentTypeListModel();
            ((EmploymentTypeListModel)Model).GetEmploymentTypeCollectionCompleted += DoGetEmploymentTypeCollectionCompleted;
            ((EmploymentTypeListModel)Model).SearhEmploymentTypeCompleted += DoSearhEmploymentTypeCompleted;
            ((EmploymentTypeListModel)Model).DeleteEmploymentTypeCompleted += DoDeleteEmploymentTypeCompleted;
            ((EmploymentTypeListModel)Model).UndoDeleteEmploymentTypeCompleted += UndoDeleteEmploymentTypeCompleted;
            ((EmploymentTypeListModel)Model).GetSingleEmploymentTypeCompleted += DoGetSingleEmploymentTypeCompleted;

            EmploymentTypeCriteria = new EmploymentType();

            ServiceCallCounter += ((EmploymentTypeListModel)Model).GetEmploymentTypeCollectionAsync(IsShowAll);

            ErrorMessageVisibility = Visibility.Collapsed;
            MessageVisibility = Visibility.Collapsed;

            CanEditOrAdd = CurrentUser.CanEditEmploymentType;
            CanAdd = CurrentUser.CanAddEmploymentType;
        }

        #region Do Completed Methods

        void DoGetSingleEmploymentTypeCompleted(object sender, SingleFeedbackCompletedEventArgs<EmploymentType> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                ErrorMessageVisibility = Visibility.Visible;
                MessageVisibility = Visibility.Collapsed;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    ErrorMessageVisibility = Visibility.Visible;
                    MessageVisibility = Visibility.Collapsed;
                }
                else
                {
                    ErrorMessageVisibility = Visibility.Collapsed;
                    MessageVisibility = Visibility.Visible;

                }
            }
        }

        void DoDeleteEmploymentTypeCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                ErrorMessageVisibility = Visibility.Visible;
                MessageVisibility = Visibility.Collapsed;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    ErrorMessageVisibility = Visibility.Visible;
                    MessageVisibility = Visibility.Collapsed;
                }
                else
                {
                    ServiceCallCounter += ((EmploymentTypeListModel)Model).GetEmploymentTypeCollectionAsync(IsShowAll);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void UndoDeleteEmploymentTypeCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                ErrorMessageVisibility = Visibility.Visible;
                MessageVisibility = Visibility.Collapsed;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    ErrorMessageVisibility = Visibility.Visible;
                    MessageVisibility = Visibility.Collapsed;
                }
                else
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.EmploymentTypeDetail, currentEmploymentTypeID);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoSearhEmploymentTypeCompleted(object sender, CollectionFeedbackCompletedEventArgs<EmploymentType> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    EmploymentTypeCollection = new PagedCollectionView(e.Result.Data);
                }
            }
        }

        void DoGetEmploymentTypeCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<EmploymentType> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                ErrorMessageVisibility = Visibility.Visible;
                MessageVisibility = Visibility.Collapsed;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    ErrorMessageVisibility = Visibility.Visible;
                    MessageVisibility = Visibility.Collapsed;
                }
                else
                {
                    ErrorMessageVisibility = Visibility.Collapsed;
                    MessageVisibility = Visibility.Visible;
                    EmploymentTypeCollection = new PagedCollectionView(e.Result.Data);
                }
            }
        }

        #endregion

        #region Commands

        #region ViewCommand

        private DelegateCommand<int?> viewCommand;
        public DelegateCommand<int?> ViewCommand
        {
            get
            {
                if (viewCommand == null)
                {
                    viewCommand = new DelegateCommand<int?>(DoViewCommand);
                }
                return viewCommand;
            }
        }

        private void DoViewCommand(int? obj)
        {
            if (obj.HasValue)
                ServiceCallCounter += ((EmploymentTypeListModel)Model).GetSingleEmploymentTypeAsync(obj.Value);
        }

        #endregion

        #region ShowAllCommand

        private DelegateCommand<object> showAllCommand;
        public DelegateCommand<object> ShowAllCommand
        {
            get
            {
                if (showAllCommand == null)
                {
                    showAllCommand = new DelegateCommand<object>(DoShowAllCommand);
                }
                return showAllCommand;
            }
        }

        private void DoShowAllCommand(object obj)
        {
            ServiceCallCounter +=
                ((EmploymentTypeListModel)Model).GetEmploymentTypeCollectionAsync(IsShowAll);
        }

        #endregion

        #region EditCommand

        private DelegateCommand<int?> editCommand;
        public DelegateCommand<int?> EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new DelegateCommand<int?>(DoEditCommand);
                }
                return editCommand;
            }
        }

        private void DoEditCommand(int? obj)
        {
            if (obj.HasValue)
            {
                currentEmploymentTypeID = obj.Value;
                var temp = EmploymentTypeCollection.Cast<EmploymentType>().FirstOrDefault(x => x.EmploymentTypeID == currentEmploymentTypeID);
                if (temp.IsDelete)
                {
                    var popup = new UndoDeleteConfirmationPopup();
                    popup.Show();
                    popup.Closed += (sender, e) =>
                    {
                        if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                        {
                            ServiceCallCounter += ((EmploymentTypeListModel)Model).UndoDeleteEmploymentTypeAsync(currentEmploymentTypeID);
                        }
                    };
                }
                else
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.EmploymentTypeDetail, currentEmploymentTypeID);
                }

            }
        }

        #endregion

        #region DeleteCommand

        private DelegateCommand<int?> deleteCommand;
        public DelegateCommand<int?> DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new DelegateCommand<int?>(DoDeleteCommand);
                }
                return deleteCommand;
            }
        }

        private void DoDeleteCommand(int? obj)
        {
            if (obj.HasValue)
            {
                var popup = new DeleteConfirmationPopup();
                popup.Show();
                popup.Closed += (sender, e) =>
                {
                    if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                    {
                        ServiceCallCounter +=
                            ((EmploymentTypeListModel)Model).DeleteEmploymentTypeAsync(obj.Value);
                    }
                };
            }


        }

        #endregion

        #region CreateCommand

        private DelegateCommand<object> createCommand;
        public DelegateCommand<object> CreateCommand
        {
            get
            {
                if (createCommand == null)
                {
                    createCommand = new DelegateCommand<object>(DoCreateCommand);
                }
                return createCommand;
            }
        }

        private void DoCreateCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.EmploymentTypeDetail);
        }

        #endregion

        #region SearchCommand

        private DelegateCommand<object> searchCommand;
        public DelegateCommand<object> SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new DelegateCommand<object>(DoSearchCommand);
                }
                return searchCommand;
            }
        }

        private void DoSearchCommand(object obj)
        {
            ServiceCallCounter += ((EmploymentTypeListModel)Model).SearchEmploymentTypeAsync(EmploymentTypeCriteria);
            Message = WebConstants.SearchingMessage;
        }

        #endregion

        #endregion

        #region Binding Properties

        private PagedCollectionView eTypeCollection;
        public PagedCollectionView EmploymentTypeCollection
        {
            get { return eTypeCollection; }
            set
            {
                if (!ReferenceEquals(eTypeCollection, value))
                {
                    eTypeCollection = value;
                    RaisePropertyChanged(() => EmploymentTypeCollection);
                }
            }
        }

        private EmploymentType eTypeCriteria;
        public EmploymentType EmploymentTypeCriteria
        {
            get { return eTypeCriteria; }
            set
            {
                if (!ReferenceEquals(eTypeCriteria, value))
                {
                    eTypeCriteria = value;
                    RaisePropertyChanged(() => EmploymentTypeCriteria);
                }
            }
        }

        #endregion

        #region Fields

        private int currentEmploymentTypeID = 0;

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            //((EmploymentTypeListModel)model).GetEmploymentTypeCollectionAsync();
        }

        public override void OnLeave()
        {
            //throw new NotImplementedException();
        }

        #endregion

        protected override void HandleLoadingCompleted()
        {

        }
    }
}